<?php 

// Include the configuration file for error management and such.
require_once ('./includes/config.inc.php'); 

// Set the page title and include the HTML header.
$page_title = 'Newnham College Associates: Administration';
$site_section = 'Admin';
include ('./includes/header.html');
include ('./includes/fetch-user.inc.php');
include ('./includes/admin.inc.php');

// is the user logged in?
if (!isset($_SESSION['user_level'])) {
  user_redirect('/login.php');
  exit(); // Quit the script.
} 
// is the user an admin?
if ($_SESSION['user_level'] < 10) {
  user_redirect('/index.php');
  exit(); // Quit the script.
} 

 // they are logged in, so we can continue
require_once ('./mysql_connect.php'); // Connect to the database.

$sort_field = 'name';
if (isset($_GET['sort'])) {$sort_field = ($_GET['sort']);}
$show = 'all';
if (isset($_GET['show'])) {$show = ($_GET['show']);}

$users=NULL;
$emails=NULL;
$user_results = fetch_sorted_user_results($show, $sort_field);
$first = TRUE;
while ($row=mysql_fetch_array($user_results)) { 
  $users[] = $row;
  if ($first) {
    $emails = $row['email'];
    $first = FALSE;
  } else {
    $emails .= ",{$row['email']}";
  }
}

mysql_close(); // Close the database connection.

?>

<h1>Administration</h1>

<h2>Special</h2>
<p class="center"><a href="assoc.php?assoc=0&do=new">Add new associate</a> 
&nbsp;&bull;&nbsp;
<a href="inactive.php">Show active/inactive associates</a></p>
<h2>Choose</h2>
<p class="center">
<a href="admin.php?show=all">Show all</a>
&nbsp;&bull;&nbsp;
<a href="admin.php?show=comm">Show committee</a>
&nbsp;&bull;&nbsp;
<a href="admin.php?show=email">Show emailable</a>
&nbsp;&bull;&nbsp;
<a href="admin.php?show=noemail">Show not emailable</a>
&nbsp;&bull;&nbsp;
<a href="admin.php?show=current">Show current</a>
&nbsp;&bull;&nbsp;
<a href="admin.php?show=ex">Show ex</a>
</p>
<h2>Operate</h2>
<p>These options act on the associates that you have chosen (i.e. the ones visible in the list below)</p>
<p class="center">
<a href="mailto:<?php echo $emails; ?>">Send email</a>
&nbsp;&bull;&nbsp;
<a href="export.php?info=addr&show=<?php echo $show;?>">Export addresses</a>
</p>

<h2>List of Associates</h2>
<p>Click on a column heading to sort the list. Click on an Associate's name to edit their details, or their email address to send them an email.</p>

<p>Showing:
<?php
if ($show == 'all') {
echo "All";
} else if ($show=='comm') {
echo "Committee";
} else if ($show=='email') {
echo "Emailable";
} else if ($show=='noemail') {
echo "Not emailable";
} else if ($show=='current') {
echo "Current Associates";
} else if ($show=='ex') {
echo "Ex Associates";
}

?>
</p>
<div class="center">
<table>
<?php
echo "<tr><td><a href=\"admin.php?sort=name&show=$show\">Name</a></td>\n";
    echo "<td><a href=\"admin.php?sort=email&show=$show\">Email</a></td>\n";
    echo "<td class=\"toright\"><a href=\"admin.php?sort=election_year&show=$show\">Election year</a></td>\n";
    echo "<td><a href=\"admin.php?sort=is_current&show=$show\">Current?</a></td>\n";
    echo "<td><a href=\"admin.php?sort=admin_email_ok&show=$show\">Use email for admin?</a></td>\n";
?>
</tr>

<?php
$grey = TRUE;
if (count($users)>0) {
    foreach ($users as $user) {
if ($grey) {
echo "<tr class=\"grey\">";
} else
echo "<tr>";
      echo "<td><a href=\"assoc.php?assoc={$user['user_id']}&do=admin\">{$user['first_name']}&nbsp;{$user['last_name']}</a></td>";
      echo "<td><a href=\"mailto:{$user['email']}\">{$user['email']}</a></td>";
      echo "<td class=\"toright\">{$user['election_year']}</td>";
      echo "<td class=\"center\">";
      echo_boolean($user['is_current'], 'Current', 'Ex');
      echo "</td>";
      echo "<td class=\"center\">";
      echo_boolean($user['admin_email_ok'], 'Yes', 'No');
      echo "</td>";
      echo "</tr>\n";
$grey= ! $grey;
    }
echo "</table>";
} else {
echo "</table>\n<p>No Associates chosen</p>\n";
}
?>
</table>
</div>

       

<?php // Include the HTML footer file.
include ('./includes/footer.html');
?>